package org.dashboard.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import org.dashboard.bean.*;
import org.dashboard.constant.Constant;
import org.dashboard.service.*;
import org.dashboard.utils.NameCastTools;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user/userpage")
public class AdUserPageController extends BaseController {

    private static final Logger logger = LoggerFactory.getLogger(AdUserPageController.class);

    @Value("${permission.pages.filter.enable}")
    private boolean pageFilterEnable;

    @Reference
    AdUserPageService adUserPageService;

    @Reference
    AdChartService adChartService;

    @Reference
    AdPageService adPageService;

    @Reference
    AdFolderService adFolderService;

    @Reference
    AdAuthorizationService adAuthorizationService;

    @RequestMapping(value = "/loadUserPage", method = {RequestMethod.POST, RequestMethod.GET}, produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public JsonResponse loadUserPage(HttpServletRequest request) {
        Integer pageId = Integer.parseInt(request.getParameter("pageId"));

        AdUser user = currentAdUser(request);

        if (pageFilterEnable) {
            if(!currentUserIsManager(request)){
                boolean flag = adAuthorizationService.checkUserHasResources(user.getUserId(), pageId, Constant.RESOURCE_TYPE_PAGE);
                if (!flag) {
                    return JsonResponse.failed();
                }
            }
        }

        AdPage adPage = adUserPageService.loadUserPage(pageId, user.getUserId());

        JsonResponse jsonResponse = new JsonResponse();
        jsonResponse.setData(NameCastTools.objectToMap(adPage));
        jsonResponse.setCode(1);
        jsonResponse.setResult("success");
        return jsonResponse;
    }


    @RequestMapping(value = "/addOrUpdate", method = {RequestMethod.POST, RequestMethod.GET}, produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public JsonResponse addOrUpdate(HttpServletRequest request) {
        Integer pageId = Integer.parseInt(request.getParameter("pageId"));
        String pageData = request.getParameter("pageData");
        AdUser user = currentAdUser(request);

        AdUserPage adUserPage = adUserPageService.addOrUpdateUserPage(user.getUserId(), pageId, pageData);

        JsonResponse jsonResponse = new JsonResponse();
        jsonResponse.setData(NameCastTools.objectToMap(adUserPage));

        jsonResponse.setCode(1);
        jsonResponse.setResult("success");
        return jsonResponse;
    }


    @RequestMapping(value = "/restoreDefault", method = {RequestMethod.POST, RequestMethod.GET}, produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public JsonResponse restoreDefault(HttpServletRequest request) {
        Integer pageId = Integer.parseInt(request.getParameter("pageId"));
        AdUser user = currentAdUser(request);

        AdPage adPage = adUserPageService.restoreDefaultUserPage(user.getUserId(), pageId);
        JsonResponse jsonResponse = new JsonResponse();
        jsonResponse.setData(NameCastTools.objectToMap(adPage));

        jsonResponse.setCode(1);
        jsonResponse.setResult("success");
        return jsonResponse;
    }


    @RequestMapping(value = "/loadUserRolePages", method = {RequestMethod.POST, RequestMethod.GET}, produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public JsonResponse loadUserRolePages(HttpServletRequest request) {
        Integer resourceType = Integer.parseInt(request.getParameter("resourceType"));
        AdUser user = currentAdUser(request);

        List<Map<String, Object>> userRolePages = adUserPageService.loadUserRolePages(user.getUserId(), resourceType);

        JsonResponse jsonResponse = new JsonResponse();
        jsonResponse.setData(userRolePages);
        jsonResponse.setCode(1);
        jsonResponse.setResult("success");
        return jsonResponse;
    }


    @RequestMapping(value = "/loadUserRoleCharts", method = {RequestMethod.POST, RequestMethod.GET}, produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public JsonResponse loadUserRoleCharts(HttpServletRequest request) {
        Integer resourceType = Integer.parseInt(request.getParameter("resourceType"));
        Integer folderType = Integer.parseInt(request.getParameter("folderType"));
        AdUser user = currentAdUser(request);


        List<Map<String, Object>> userRoleCharts = adChartService.loadUserRoleCharts(user.getUserId(), resourceType);
        List<AdFolder> folders = adFolderService.selectAdFoldersByType(folderType);

        JsonResponse jsonResponse = new JsonResponse();
        Map<String, Object> map = new HashMap<>();

        map.put("charts", userRoleCharts);
        map.put("folders", NameCastTools.listToListMap(folders));

        jsonResponse.setData(map);
        jsonResponse.setCode(1);
        jsonResponse.setResult("success");
        return jsonResponse;
    }

}
